package com.haiyou.common.thread.task;

import java.util.concurrent.TimeUnit;

import com.google.common.base.Stopwatch;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 
 * @Description: Runnable 线程方式
 * @author xingyuan
 * @date 2022年7月6日 下午4:32:17
 */
@Slf4j
public abstract class AbstractRunnableCoreTask extends AbstractCoreTask implements Runnable{

	@Override
	public void run() {
		Stopwatch stopwatch = Stopwatch.createStarted();
		try {
			 exec();
		} catch (Exception e) {
			log.error(ex.getMessage(), ex);// 任务提交堆栈处
			log.error(e.getMessage(), e);// 异常出现堆栈处
		} finally {
			long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
			if (elapsed > executeSlowly())
				log.warn("SLOWLY: {}, used {} ms", this, elapsed);
			else {
				if(log.isDebugEnabled()) {
					log.debug("{}, used {} ms", this, elapsed);
				}
			}
			
		}
	}

	protected abstract void exec() throws Exception;
}
